package com.lancoo.ccas53.arrangecourse.dataprocess;

import com.lancoo.ccas53.arrangecourse.entities.SplitCourse;
import com.lancoo.ccas53.arrangecourse.entities.TeachingClassUnit;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: liufeng
 * @CreateTime: 2024-09-11  19:16
 * @Description:
 */
@Slf4j
public class GenerateMergeClassGroup {

    public LinkedList<TeachingClassUnit> start(LinkedList<TeachingClassUnit> teachingClassUnitList) {
        LinkedList<TeachingClassUnit> groupedList = new LinkedList<>();
        if (teachingClassUnitList != null && !teachingClassUnitList.isEmpty()) {
            Map<Long, LinkedList<TeachingClassUnit>> classUnitMap = teachingClassUnitList.stream()
                    .collect(Collectors.groupingBy(TeachingClassUnit::getTeachingClassId, Collectors.toCollection(LinkedList::new)));

            //按照教学班，一个个去遍历待排教学班排课单元map
            for (Map.Entry<Long, LinkedList<TeachingClassUnit>> entry : classUnitMap.entrySet()) {
                LinkedList<TeachingClassUnit> retList = generateClassUnitGroup(entry.getValue());
                //将配对的结果，加入分组后的列表
                groupedList.addAll(retList);
            }

        }
        log.error("#GenerateMergeClassGroup.getClassUnitGroupMap# 传入的教学班排课单元列表为空，无需进行分组，请排查原因~");
        return groupedList;

    }

    private LinkedList<TeachingClassUnit> generateClassUnitGroup(LinkedList<TeachingClassUnit> classUnitList) {
        if (classUnitList == null || classUnitList.isEmpty()) {
            log.error("#GenerateMergeClassGroup.generateClassUnitGroup# 传入的教学班排课单元列表为空，无需进行分组，请排查原因~");
            return null;
        }
        //定义用来保存已经标记分组的教学班排课单元列表
        LinkedList<TeachingClassUnit> groupedList = new LinkedList<>();
//        Map<Integer, LinkedList<TeachingClassUnit>> connectSectionMap = classUnitList.stream()
//                .collect(Collectors.groupingBy(TeachingClassUnit::getConnectSection, Collectors.toCollection(LinkedList::new)));
//        for (LinkedList<TeachingClassUnit> value : connectSectionMap.values()) {
//            Map<Integer, LinkedList<TeachingClassUnit>> type = value.stream().collect(Collectors.groupingBy(TeachingClassUnit::getTeachingType, Collectors.toCollection(LinkedList::new)));
//
//        }
        while (true) {
            if (classUnitList.isEmpty()) {
                break;
            }

            LinkedList<TeachingClassUnit> canGroup = new LinkedList<>();
            TeachingClassUnit item = classUnitList.get(0);
            classUnitList.remove(item);
            canGroup.add(item);

            Map<Integer, List<TeachingClassUnit>> typeMap = classUnitList.stream().filter(o -> !item.getTeachingType().equals(o.getTeachingType())
                    && item.getConnectSection().equals(o.getConnectSection())).collect(Collectors.groupingBy(TeachingClassUnit::getTeachingType));

            for (List<TeachingClassUnit> value : typeMap.values()) {
                canGroup.add(value.get(0));
            }
            classUnitList.removeAll(canGroup);
            groupedList.addAll(startGroup(canGroup));

//            Iterator iterator = classUnitList.iterator();
//            while (iterator.hasNext()) {
//                //获取当前迭代的课程
//                TeachingClassUnit unit = (TeachingClassUnit) iterator.next();
//                //如果拆班教学班，奇数教学班数量大于0，才进行分组
//                if (!item.getTeachingType().equals(unit.getTeachingType())
//                        && item.getConnectSection().equals(unit.getConnectSection())
//                ) {
//                    groupedList = startGroup(item, unit, groupedList);
//
//                }
//            }

        }

        return groupedList;
    }

    private LinkedList<TeachingClassUnit> startGroup(LinkedList<TeachingClassUnit> canGroup) {
        //生成一个UUID，作为分组id
        String splitClassGroupId = UUID.randomUUID().toString();
        LinkedList<TeachingClassUnit> groupedList = new LinkedList<>();
        canGroup.forEach(o -> {
            o.setSplitClassGroupId(splitClassGroupId);
            groupedList.add(o);
        });


        return groupedList;
    }
}
